package online.wenmeng.service;

import com.sun.mail.imap.protocol.ID;
import online.wenmeng.bean.*;
import online.wenmeng.config.Config;
import online.wenmeng.dao.*;
import online.wenmeng.exception.ParameterErrorException;
import online.wenmeng.utils.MyUtils;
import org.apache.ibatis.binding.MapperMethod;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class AdminService {
    
    @Autowired
    FilmuserMapper filmuserMapper;
    @Autowired
    MoviesMapper moviesMapper;
    @Autowired
    CommentsMapper commentsMapper;
    @Autowired
    DiscussionitemMapper discussionitemMapper;
    @Autowired
    DiscussionMapper discussionMapper;
    @Autowired
    SpellgroupsMapper spellgroupsMapper;
    @Autowired
    SpellgroupsuserMapper spellgroupsuserMapper;

    public Map<String, Object> findAllUser() {
        FilmuserExample filmuserExample = new FilmuserExample();
        List<Filmuser> filmuserList = filmuserMapper.selectByExample(filmuserExample);
        return MyUtils.getNewMap(Config.SUCCESS,null,null,filmuserList);
    }

    public Map<String, Object> delUser(int userId) {
        int i = filmuserMapper.deleteByPrimaryKey(userId);
        if (i>0){
            return findAllUser();
        }
        return MyUtils.getNewMap(Config.ERROR,null,"not have the user",userId);
    }

    /**
     * 删除电影，电影下评论一起删
     * @param movieId
     * @return
     */
    public Map<String, Object> delMovie(int movieId) {
        int i = moviesMapper.deleteByPrimaryKey(movieId);
        if(i>0){
            //删除所有电影评论
            CommentsExample commentsExample = new CommentsExample();
            commentsExample.createCriteria().andMoveidEqualTo(movieId);
            int deleteCommentNum = commentsMapper.deleteByExample(commentsExample);
            MoviesExample moviesExample = new MoviesExample();
            List<Movies> movies = moviesMapper.selectByExample(moviesExample);
            return MyUtils.getNewMap(Config.SUCCESS,movieId,deleteCommentNum,movies);
        }
        return MyUtils.getNewMap(Config.ERROR,null,"not have the movie",movieId);
    }

    /**
     * 删除电影评论
     *      连带减少电影评论数量
     * @param commentsId
     * @return
     */
    public Map<String, Object> delComments(int commentsId) throws ParameterErrorException {
        Comments comments= commentsMapper.selectByPrimaryKey(commentsId);
        if (comments!=null){
            commentsMapper.deleteByPrimaryKey(commentsId);
            //将减少电影评论数量
            Movies movies = moviesMapper.selectByPrimaryKey(comments.getMoveid());
            movies.setCommentcounts(movies.getCommentcounts()>0?movies.getCommentcounts()-1:0);
            moviesMapper.updateByPrimaryKey(movies);
            CommentsExample commentsExample = new CommentsExample();
            commentsExample.createCriteria().andMoveidEqualTo(comments.getMoveid());
            List<Comments> commentsList = commentsMapper.selectByExample(commentsExample);
            return MyUtils.getNewMap(Config.SUCCESS,comments,movies,commentsList);
        }
        throw new ParameterErrorException();
    }

    /**
     * 查询电影所有评论
     * @return
     */
    public Map<String, Object> findAllComments(int movieId) throws ParameterErrorException {
        Movies movies = moviesMapper.selectByPrimaryKey(movieId);
        if (movies==null){
            throw new ParameterErrorException();
        }
        CommentsExample commentsExample = new CommentsExample();
        commentsExample.createCriteria().andMoveidEqualTo(movieId);
        List<Comments> comments = commentsMapper.selectByExample(commentsExample);
        return MyUtils.getNewMap(Config.SUCCESS,movieId,movies,comments);
    }




    public Map<String, Object> delDiscussionitem(int discussionitemId) throws ParameterErrorException {
        Discussionitem discussionitem = discussionitemMapper.selectByPrimaryKey(discussionitemId);
        if (discussionitem==null){
            throw new ParameterErrorException();
        }
        int i = discussionitemMapper.deleteByPrimaryKey(discussionitemId);
        if (i>0){
            //减少评论数量
            Discussion discussion = discussionMapper.selectByPrimaryKey(discussionitem.getDiscussionid());
            if (discussion!=null){
                discussion.setFilmcount(discussion.getFilmcount()>0?discussion.getFilmcount()-1:0);
                discussionMapper.updateByPrimaryKey(discussion);
            }
            return MyUtils.getNewMap(Config.SUCCESS,discussionitem,discussion,findDiscussion(discussion.getDiscussionid()).get(Config.DATA));
        }
        return MyUtils.getNewMap(Config.ERROR,Config.RETRY,i,discussionitem);
    }



    public Map<String, Object> delDiscussion(int discussionId) throws ParameterErrorException {
        Discussion discussion = discussionMapper.selectByPrimaryKey(discussionId);
        if (discussion==null){
            throw new ParameterErrorException();
        }
        int i = discussionMapper.deleteByPrimaryKey(discussionId);
        if (i>0){
            //删除对应的所有评论
            DiscussionitemExample discussionitemExample = new DiscussionitemExample();
            discussionitemExample.createCriteria().andDiscussionidEqualTo(discussionId);
            int i1 = discussionitemMapper.deleteByExample(discussionitemExample);
            return MyUtils.getNewMap(Config.SUCCESS,discussion,i+"_"+i1,discussionMapper.selectByExample(new DiscussionExample()));
        }
        return MyUtils.getNewMap(Config.ERROR,Config.RETRY,i,discussion);
    }

    public Map<String, Object> delSpellgroups(int spellgroupsId) throws ParameterErrorException {
        Spellgroups spellgroups = spellgroupsMapper.selectByPrimaryKey(spellgroupsId);
        if (spellgroups==null){
            throw new ParameterErrorException();
        }
        int i = spellgroupsMapper.deleteByPrimaryKey(spellgroupsId);
        if (i>0){
            //删除用户中的团约信息
            SpellgroupsuserExample spellgroupsuserExample = new SpellgroupsuserExample();
            spellgroupsuserExample.createCriteria().andSpellgroupidEqualTo(spellgroupsId);
            int i1 = spellgroupsuserMapper.deleteByExample(spellgroupsuserExample);
            //返回所有的团约信息
            return MyUtils.getNewMap(Config.SUCCESS,spellgroups,i+"_"+i1,spellgroupsuserMapper.selectByExample(new SpellgroupsuserExample()));
        }
        return MyUtils.getNewMap(Config.ERROR,Config.RETRY,i,spellgroups);
    }

    public Map<String, Object> findDiscussion(int discussionId) throws ParameterErrorException {
        Discussion discussion = discussionMapper.selectByPrimaryKey(discussionId);
        if (discussion==null){
            throw new ParameterErrorException();
        }
        DiscussionitemExample discussionitemExample = new DiscussionitemExample();
        discussionitemExample.createCriteria().andDiscussionidEqualTo(discussionId);
        List<Discussionitem> discussionitems = discussionitemMapper.selectByExample(discussionitemExample);
        return MyUtils.getNewMap(Config.SUCCESS,discussion,discussionId,discussionitems);
    }
}
